<template>
  <div class="user-chat-page">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="小智同学"
    ></van-nav-bar>
    <!-- 聊天列表 -->
    <div class="chat-list" ref="list">
      <div v-for="(item, index) in list" :key="index">
        <!-- 小智 -->
        <div class="chat-item left" v-if="item.local == 'left'">
          <geek-icon name="xiaozhi" />
          <div class="chat-pao">{{ item.msg }}</div>
        </div>
        <!-- 我 -->
        <div class="chat-item right" v-if="item.local === 'right'">
          <div class="chat-pao">{{ item.msg }}</div>
          <van-image
            fit="cover"
            round
            :src="$store.state.user.userInfo.photo"
          />
        </div>
      </div>
    </div>
    <!-- 输入框 -->
    <div class="reply-container van-hairline--top">
      <van-field
        v-model.trim="value"
        left-icon="edit"
        placeholder="请描述您的问题"
      ></van-field>
      <span class="send" @click="send">发送</span>
    </div>
  </div>
</template>

<script>
// import { getUserProfile } from '@/api/user'
import socketIo from 'socket.io-client'
export default {
  name: 'UserChatPage',
  data () {
    return {
      io: '',
      list: [
        // {
        //   local: 'left',
        //   msg: '你好，靓仔'
        // },
        // {
        //   local: 'right',
        //   msg: '小智，以后买单的事都交给你了'
        // }
      ],
      value: ''
    }
  },
  async created () {
    this.$store.dispatch('user/getUserInfo')
    // ws链接url是链接基地址
    this.io = socketIo(process.env.VUE_APP_URL, {
      query: {
        token: this.$store.state.user.token
      },
      transports: ['websocket']
    })
    // 监听连接是否成功
    this.io.on('connect', () => {
      this.list.push({
        local: 'left',
        msg: '您好，靓仔有没有忙帮能到你的？'
      })
    })
    // 监听消息
    this.io.on('message', obj => {
      this.list.push({
        local: 'left',
        msg: obj.msg
      })
      this.$nextTick(() => {
        this.$refs.list.scrollTop = 9999999
      })
    })
  },
  beforeDestroy () {
    // 关闭链接
    this.io.close()
  },
  methods: {
    // 发送消息
    send () {
      if (this.value !== '') {
        this.io.emit('message', {
          msg: this.value,
          timestamp: Date.now()
        })
        this.list.push({
          local: 'right',
          msg: this.value
        })
        this.value = ''
        this.$nextTick(() => {
          this.$refs.list.scrollTop = 9999999
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
.user-chat-page {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fff;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: middle;
        width: 40px;
        height: 40px;
      }
      .geek-icon {
        font-size: 40px;
        line-height: 0;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 20px;
        border-radius: 4px;
        position: relative;
        padding: 10px;
        background-color: @geek-gray-color;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: '';
          width: 6px;
          height: 6px;
          position: absolute;
          top: 15px;
          background: @geek-gray-color;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 10px;
    &::before {
      right: -3px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 10px;
    margin-right: 0;
    &::before {
      left: -3px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 49px;
  width: 100%;
  background: #fff;
  z-index: 9999;
  display: flex;
  align-items: center;
  padding: 0 10px;
  > .van-field {
    flex: 1;
    background: #f7f8fa;
    height: 32px;
    border-radius: 16px;
    padding: 0 10px;
    line-height: 32px;
    ::v-deep .van-field__left-icon .van-icon {
      color: #ccc;
    }
  }
  > .send {
    margin-left: 10px;
    font-size: 14px;
    color: #999;
  }
}
</style>
